﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_HappyEvaluateUpdForm
 * 类型：       控制层
 * 功能说明：   快乐管家-活动计划评价登记页面
 * 作者：       郭晓庆
 * 创建日期：   2007/09/12
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using log4net;
using Shpm.App.Commons.Utils;
using Shpm.App.Master.Dao;
using Shpm.System.Validator;
using Shpm.System.Web.Control;
using Shpm.App.Master.Bean;

public partial class App_Input_HappyEvaluateUpdForm : System.Web.UI.Page
{
    private const string ACTION_EVALUATE_MEMBER = "ACTION_EVALUATE_MEMBER";
    private const string ACTION_EVALUATE_EMPLOYEE = "ACTION_EVALUATE_EMPLOYEE";
    private const string SCORE_TYPE = "SCORE_TYPE";
    // log
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_HappyEvaluateUpdForm));
  
    // page load
    protected void Page_Load(object sender, EventArgs e)
    {        
        
        if (!Page.IsPostBack)
        {
            // 保存接收的参数
            ViewState["InpActName"] = Convert.ToString(Context.Items["InpActName"]);
            ViewState["InpActType"] = Convert.ToString(Context.Items["InpActType"]);
            ViewState["InpActStat1"] = Convert.ToString(Context.Items["InpActStat1"]);
            ViewState["InpActStat2"] = Convert.ToString(Context.Items["InpActStat2"]);
            ViewState["InpBeginDate"] = Convert.ToString(Context.Items["InpBeginDate"]);
            ViewState["InpEndDate"] = Convert.ToString(Context.Items["InpEndDate"]);

            ViewState["HidActName"] = Convert.ToString(Context.Items["HidActName"]);
            ViewState["HidActType"] = Convert.ToString(Context.Items["HidActType"]);
            ViewState["HidActStat1"] = Convert.ToString(Context.Items["HidActStat1"]);
            ViewState["HidActStat2"] = Convert.ToString(Context.Items["HidActStat2"]);
            ViewState["HidBeginDate"] = Convert.ToString(Context.Items["HidBeginDate"]);
            ViewState["HidEndDate"] = Convert.ToString(Context.Items["HidEndDate"]);
            ViewState["HidCommand"] = Convert.ToString(Context.Items["HidCommand"]);

            if (Context.Items.Contains("HidPageIndex"))
            {
                ViewState["HidPageIndex"] = Context.Items["HidPageIndex"];
            }


            //接收活动编号
            int actionId = Convert.ToInt32(Context.Items["ACTION_ID"]);
            this.labActinId.Text = Convert.ToString(actionId);




            //检索活动信息
            HappyPlanDao happyPlanDao = new HappyPlanDao();
            HappyPlanBean bean = new HappyPlanBean();
          

            bean.ActionId = actionId;
            bean = happyPlanDao.FindHappyPlanInfo(bean);
            //活动类型
            string evluateType = bean.ActionType;

            this.labActionName.Text=bean.ActionName;
            this.labRespPesonName.Text=bean.RespPesonName;
            this.labTypeName.Text=bean.TypeName;
            this.labActionDate.Text = DateUtils.convertToString((DateTime)bean.BespokeDate);
            this.labActionAddress.Text = bean.ActionAddress;





            //会员评价gridview
            InitMemberAppraise();

            //职员评价gridview
            InitEmployeeAppraise();
            //会员评价类型
            CommonDictionaryBean paramBean = new CommonDictionaryBean();
            switch(evluateType)
            {
                case "A":
                    paramBean.Catalog = "T_ACTION_EVALUATE_TYPE_A";
                    break;
                case "C":
                    paramBean.Catalog = "T_ACTION_EVALUATE_TYPE_C";
                    break;
                case "E":
                    paramBean.Catalog = "T_ACTION_EVALUATE_TYPE_E";
                    break;
                case "B":
                    paramBean.Catalog = "T_ACTION_EVALUATE_TYPE_A";
                    break;
                case "F":
                    paramBean.Catalog = "T_ACTION_EVALUATE_TYPE_A";
                    break;
            }

            //保存评价类型
            ViewState["EVALUATE_TYPE"] = paramBean.Catalog;
            
            //会员评价表检索
            CommonDictionaryDao dao = new CommonDictionaryDao();
            IList dataList = dao.FindByCatalog(paramBean);
            ViewState[ACTION_EVALUATE_MEMBER] = dataList;

            //员工评价表检索
            paramBean.Catalog = "T_ACTION_EVALUATE_TYPE_G";
            dataList = dao.FindByCatalog(paramBean);
            ViewState[ACTION_EVALUATE_EMPLOYEE] = dataList;


            //评分标准检索
            paramBean.Catalog = "EVALUATE_SCORE";
            dataList = dao.FindByCatalog(paramBean);
            ViewState[SCORE_TYPE] = dataList;

           
            //setView
            MultiView1.SetActiveView(View1);
            this.txtEmployeeNo.Attributes.Add("onblur", "showEmployeeName(this.id,'" + this.txtEmployeeName.ClientID + "');");

        }
        if (ViewState.IsItemDirty(ACTION_EVALUATE_MEMBER))
        {
            RestoreItemControls((IList)ViewState[ACTION_EVALUATE_MEMBER],(IList)ViewState[SCORE_TYPE]);
            RestoreItemControls2((IList)ViewState[ACTION_EVALUATE_EMPLOYEE], (IList)ViewState[SCORE_TYPE]);
        }
      
    }

    private void InitEmployeeAppraise()
    {
        HappyEvaluateDao dao = new HappyEvaluateDao();
        ActionEmployeeAppraiseBean bean = new ActionEmployeeAppraiseBean();
        bean.ActionId = Convert.ToInt32(this.labActinId.Text);
        IList list = dao.FindActionEmployee(bean);
        this.GridView2.DataSource = list;
        this.GridView2.DataBind();
    }

    private void InitMemberAppraise()
    {
        HappyEvaluateDao dao = new HappyEvaluateDao();
        ActionMemberAppraiseBean bean = new ActionMemberAppraiseBean();
        bean.ActionId = Convert.ToInt32(this.labActinId.Text);
        IList list = dao.FindActionMember(bean);
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();
    }


    protected void rabListPersonType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (rabListPersonType.SelectedIndex == 1)
        {
            MultiView1.SetActiveView(View2);
        }
        else { MultiView1.SetActiveView(View1); }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ActionMemberAppraiseBean bean = (ActionMemberAppraiseBean)e.Row.DataItem;
            e.Row.Cells[3].Text = string.IsNullOrEmpty(bean.AppraiseDate) ? "未评":"已评";
            
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int row = (sender as GridView).SelectedIndex;
        ViewState["MEMBER_NO"] = (sender as GridView).DataKeys[row].Value.ToString();
        this.labMemberName.Text = (sender as GridView).Rows[row].Cells[1].Text;
        ViewState["MEMBER_SELECT"] = "TRUE";
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        GridView1.PageIndex = e.NewPageIndex;
        InitMemberAppraise();
    }

    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ActionEmployeeAppraiseBean bean = (ActionEmployeeAppraiseBean)e.Row.DataItem;
            e.Row.Cells[3].Text = string.IsNullOrEmpty(bean.AppraiseDate) ? "未评" : "已评";

        }
    }
    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {
        int row = (sender as GridView).SelectedIndex;
        ViewState["EMPLOYEE_ID"] = (sender as GridView).DataKeys[row].Value.ToString();
        this.txtEmployeeNo.Text = (sender as GridView).DataKeys[row].Value.ToString();
        this.txtEmployeeName.Text = (sender as GridView).Rows[row].Cells[1].Text;
        //this.labEmployeeName.Text = (sender as GridView).Rows[row].Cells[1].Text;
        ViewState["EMPLOYEE_SELECT"] = "TRUE";
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        GridView2.PageIndex = e.NewPageIndex;
        InitEmployeeAppraise();


    }


    /*名称： RestoreItemControls
     *功能： 
     *参数： 无
     *返回： 无
     *说明： 无
     */
    protected void RestoreItemControls(IList evluateList,IList scoreList)
    {
        CommonDictionaryBean bean=null;
        ActionMemberAppraiseBean appraiseBean = null;
        IList appraiseList = ViewState.IsItemDirty("MEMBER_APPRAISE") ? (IList)ViewState["MEMBER_APPRAISE"]:null;
        for (int i = 0; i < evluateList.Count; i++)
        {
            bean = (CommonDictionaryBean)evluateList[i];
            if (appraiseList != null)
            {
                appraiseBean = (ActionMemberAppraiseBean)appraiseList[i];
            }
            TableRow tblRow = new TableRow();

            TableCell tblCell;

            //标题
            tblCell = new TableCell();
            Label labName = new Label();
            labName.Text = bean.Content;
            //tblCell.Text = bean.Content;
            HiddenField hideMemberAppraiseType = new HiddenField();
            hideMemberAppraiseType.ID = string.Format("hideMemberAppraiseType{0}", i);
            hideMemberAppraiseType.Value = bean.DictNo;
            tblCell.Controls.Add(labName);
            tblCell.Controls.Add(hideMemberAppraiseType);
            tblRow.Cells.Add(tblCell);



            //评分
            tblCell = new TableCell();
            RadioButtonList radList = new RadioButtonList();
            radList.ID = string.Format("radLMemberScore_{0}", i);
            radList.EnableViewState = true;
            radList.RepeatDirection = RepeatDirection.Horizontal;
            radList.DataTextField = "Content";
            radList.DataValueField = "DictNo";
            radList.DataSource = scoreList;
            radList.DataBind();
            if (scoreList != null && scoreList.Count > 0)
            {
                radList.SelectedValue = radList.Items.FindByValue("3") == null ? null : "3";
            }
            if (appraiseList != null)
            {
                radList.SelectedValue = appraiseBean.AppraiseLevel;
            }

            tblCell.Controls.Add(radList);
            tblRow.Cells.Add(tblCell);


            //原因分析

            tblCell = new TableCell();
            TextBox txtAppraiseCausation = new TextBox();
            txtAppraiseCausation.TextMode = TextBoxMode.MultiLine;
            txtAppraiseCausation.ID = string.Format("txtAppraiseCausation_{0}", i);
            txtAppraiseCausation.EnableViewState = true;


            txtAppraiseCausation.Text = appraiseList != null? appraiseBean.AppraiseCausation:"";

            tblCell.Controls.Add(txtAppraiseCausation);
            tblRow.Cells.Add(tblCell);

            //改进意见
            tblCell = new TableCell();
            TextBox txtAppraiseIdea = new TextBox();
            txtAppraiseIdea.TextMode = TextBoxMode.MultiLine;
            txtAppraiseIdea.ID = string.Format("txtAppraiseIdea_{0}", i);
            txtAppraiseIdea.EnableViewState = true;

            txtAppraiseIdea.Text = appraiseList != null ? appraiseBean.AppraiseIdea : "";
            
            tblCell.Controls.Add(txtAppraiseIdea);
            tblRow.Cells.Add(tblCell);

            TblItem.Rows.Add(tblRow);
        }

    }
    /*名称： RestoreItemControls
   *功能： 
   *参数： 无
   *返回： 无
   *说明： 无
   */
    protected void RestoreItemControls2(IList evluateList, IList scoreList)
    {
        CommonDictionaryBean bean = null;
        ActionEmployeeAppraiseBean appraiseBean = null;
        IList appraiseList = ViewState.IsItemDirty("EMPLOYEE_APPRAISE") ? (IList)ViewState["EMPLOYEE_APPRAISE"] : null;
        for (int i = 0; i < evluateList.Count; i++)
        {
            bean = (CommonDictionaryBean)evluateList[i];

            if (appraiseList != null)
            {
                appraiseBean = (ActionEmployeeAppraiseBean)appraiseList[i];
            }
            TableRow tblRow = new TableRow();

            TableCell tblCell;

            //标题
            tblCell = new TableCell();
            Label labName = new Label();
            labName.Text = bean.Content;
            //tblCell.Text = bean.Content;
            HiddenField hideEmployeeAppraiseType = new HiddenField();
            hideEmployeeAppraiseType.ID = string.Format("hideEmployeeAppraiseType{0}", i);
            hideEmployeeAppraiseType.Value = bean.DictNo;
            tblCell.Controls.Add(labName);
            tblCell.Controls.Add(hideEmployeeAppraiseType);
            tblRow.Cells.Add(tblCell);



            //评分
            tblCell = new TableCell();
            RadioButtonList radList = new RadioButtonList();
            radList.RepeatDirection = RepeatDirection.Horizontal;
            radList.ID = string.Format("radLEmployeeScore_{0}", i);
            radList.DataTextField = "Content";
            radList.DataValueField = "DictNo";
            radList.DataSource = scoreList;
            radList.DataBind();
            if (scoreList != null && scoreList.Count >0)
            {
                radList.SelectedValue = radList.Items.FindByValue("3") == null ? null : "3";
            }
            if (appraiseList != null)
            {
                radList.SelectedValue = appraiseBean.AppraiseLevel;
            }
            tblCell.Controls.Add(radList);
            tblRow.Cells.Add(tblCell);


            //原因分析

            tblCell = new TableCell();
            TextBox txtAppraiseCausationE = new TextBox();
            txtAppraiseCausationE.TextMode = TextBoxMode.MultiLine;
            txtAppraiseCausationE.ID = string.Format("txtAppraiseCausationE_{0}", i);
            txtAppraiseCausationE.EnableViewState = true;
            txtAppraiseCausationE.Text = appraiseList != null ? appraiseBean.AppraiseCausation : "";
            tblCell.Controls.Add(txtAppraiseCausationE);
            tblRow.Cells.Add(tblCell);

            //改进意见
            tblCell = new TableCell();
            TextBox txtAppraiseIdeaE = new TextBox();
            txtAppraiseIdeaE.TextMode = TextBoxMode.MultiLine;
            txtAppraiseIdeaE.ID = string.Format("txtAppraiseIdeaE_{0}", i);
            txtAppraiseIdeaE.EnableViewState = true;

            txtAppraiseIdeaE.Text = appraiseList != null ? appraiseBean.AppraiseIdea : "";
            tblCell.Controls.Add(txtAppraiseIdeaE);
            tblRow.Cells.Add(tblCell);

            TblItem2.Rows.Add(tblRow);
        }
       
    

    }
    /*名称： ClearItemControls
    *功能： 清空配置控件
    *参数： 无
    *返回： 无
    *说明： 无
    */
    protected void ClearItemControls()
    {
        

            while (TblItem.Rows.Count > 1)
            {
                TblItem.Rows.RemoveAt(1);
            }

        
    }
    /*名称： ClearItemControls2
   *功能： 清空配置控件
   *参数： 无
   *返回： 无
   *说明： 无
   */
    protected void ClearItemControls2()
    {


        while (TblItem2.Rows.Count > 1)
        {
            TblItem2.Rows.RemoveAt(1);
        }


    }
    /*名称： OnPreRender
     *功能： 此方法通知服务器控件在保存视图状态和呈现内容之前，执行任何必要的预呈现步骤。
     *参数： e - 包含事件数据的 EventArgs 对象
     *返回： 无
     *说明： 无
     */
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        if (ViewState.IsItemDirty("MEMBER_SELECT"))
        {
            HappyEvaluateDao dao = new HappyEvaluateDao();
            ActionMemberAppraiseBean bean = new ActionMemberAppraiseBean();
            bean.ActionId = Convert.ToInt32(this.labActinId.Text);
            bean.MemberNo = Convert.ToString(ViewState["MEMBER_NO"]);
            bean.Catalog = Convert.ToString(ViewState["EVALUATE_TYPE"]);
            IList list = dao.FindActionMemberAppraise(bean);
            if (list != null && list.Count != 0)
            {
                //查找备注
                HappyIntentDao happyIntentDao = new HappyIntentDao();
                ActionMemberBean actionMemberBean = new ActionMemberBean();
                actionMemberBean.ActionId = Convert.ToInt32(this.labActinId.Text);
                actionMemberBean.MemberNo = Convert.ToString(ViewState["MEMBER_NO"]);
                actionMemberBean = happyIntentDao.LoadActionMember(actionMemberBean);
                this.txtMemberMemo.Text = actionMemberBean.AppraiseMemo == null ? "" : actionMemberBean.AppraiseMemo;

                ViewState["MEMBER_APPRAISE"] = list;
            }
            else 
            {
                ViewState.Remove("MEMBER_APPRAISE");
            }
            RadioButton r = new RadioButton();
            
            ClearItemControls();
            RestoreItemControls((IList)ViewState[ACTION_EVALUATE_MEMBER], (IList)ViewState[SCORE_TYPE]);
            ViewState.Remove("MEMBER_SELECT");
        }
        else if (ViewState.IsItemDirty("EMPLOYEE_SELECT"))
        {
            HappyEvaluateDao dao = new HappyEvaluateDao();
            ActionEmployeeAppraiseBean bean = new ActionEmployeeAppraiseBean();
            bean.ActionId = Convert.ToInt32(this.labActinId.Text);
            bean.EmployeeId = Convert.ToString(ViewState["EMPLOYEE_ID"]);
            bean.Catalog = "T_ACTION_EVALUATE_TYPE_G";
            IList list = dao.FindActionEmployeeAppraise(bean);

            if (list != null && list.Count != 0)
            {
                this.txtEmployeeMemo.Text = (list[0] as ActionEmployeeAppraiseBean).AppraiseMemo;
                ViewState["EMPLOYEE_APPRAISE"] = list;
            }
            else
            {
                this.txtEmployeeMemo.Text = "";
                ViewState.Remove("EMPLOYEE_APPRAISE");
            }
            RadioButton r = new RadioButton();

            ClearItemControls2();
            RestoreItemControls2((IList)ViewState[ACTION_EVALUATE_EMPLOYEE], (IList)ViewState[SCORE_TYPE]);
            ViewState.Remove("EMPLOYEE_SELECT");
        }
      //  ClearItemControls();
        //RestoreItemControls();
    }

    #region ***************会员评价保存*****************************
    protected void memberSave_Click(object sender, EventArgs e)
    {
        //会员编号检查 --false:未选择会员;true:已经选择
        if( string.IsNullOrEmpty(this.labMemberName.Text))
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                         HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0100").ToString().Replace("{0}", "会员");
            return;
        }

        
        if (this.TblItem.Rows.Count<=1)
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                         HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0029").ToString().Replace("{0}", "评价不存在");
            return;
        }


        //保存
        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        HappyEvaluateDao dao = new HappyEvaluateDao();
        ActionMemberAppraiseBean bean = new ActionMemberAppraiseBean();
        bean.MemberNo = Convert.ToString(ViewState["MEMBER_NO"]);
        bean.ActionId = Convert.ToInt32(this.labActinId.Text);
        bean.AppraiseMemo = this.txtMemberMemo.Text;
        bean.UserId = manager.getSysUserBean().getMstSysUserBean().SysUserId;
        dao.InsertMember(TblItem,bean);

        ((Design_MasterPage)this.Master).InfoMessage =
                            HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();


        InitMemberAppraise();
    }
    # endregion 

    #region ***************员工评价保存*****************************
    protected void employeeSave_Click(object sender, EventArgs e)
    {
        ComValidateChecker vd = new ComValidateChecker();
        vd.CheckItems.Add(new CheckRequired(this.txtEmployeeNo.Text.Trim(), new ComMessage("E0094", "员工编号")));
        if(!vd.DoValidator())
        {
            ((Design_MasterPage)this.Master).InfoMessage = vd.ErrorMessage;
            return;
        }

        //员工编号存在检查
        if( !checkEmployeeID(this.txtEmployeeNo.Text.Trim()))
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0032").ToString().Replace("{0}", this.txtEmployeeNo.Text.Trim());
            return;
        }

        //会员编号检查 --false:未选择会员;true:已经选择
        /*if (string.IsNullOrEmpty(this.labEmployeeName.Text))
        {
            ((Design_MasterPage)this.Master).InfoMessage =
                         HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "E0100").ToString().Replace("{0}", "员工");
            return;
        }*/


        //保存
        SysUserManager manager = new SysUserManager(Context);
        SysUserBean sysUserBean = manager.getSysUserBean();
        HappyEvaluateDao dao = new HappyEvaluateDao();
        ActionEmployeeAppraiseBean bean = new ActionEmployeeAppraiseBean();
        bean.EmployeeId = this.txtEmployeeNo.Text.Trim();
        bean.ActionId = Convert.ToInt32(this.labActinId.Text);
        bean.AppraiseMemo = this.txtEmployeeMemo.Text;
        bean.UserId = manager.getSysUserBean().getMstSysUserBean().SysUserId;
        dao.InsertEmployee(TblItem2, bean);

        ((Design_MasterPage)this.Master).InfoMessage =
                            HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();


        InitEmployeeAppraise();
    }
    # endregion 


    private bool checkEmployeeID(string id)
    {
        if (string.IsNullOrEmpty(id))
            return true;

        EmployeeDao dao = new EmployeeDao();
        EmployeeBean bean = new EmployeeBean();
        bean.EmployeeId = id;
        if (dao.GetEmployee(bean) == null)
            return false;

        return true;

    }
    protected void back_Click(object sender, EventArgs e)
    {
        string url = "~/App/Input/HappyPlanSrhForm.aspx";

        // 回传接收的参数       
        Context.Items["InpActName"] = ViewState["InpActName"];
        Context.Items["InpActType"] = ViewState["InpActType"];
        Context.Items["InpActStat1"] = ViewState["InpActStat1"];
        Context.Items["InpActStat2"] = ViewState["InpActStat2"];
        Context.Items["InpBeginDate"] = ViewState["InpBeginDate"];
        Context.Items["InpEndDate"] = ViewState["InpEndDate"];

        Context.Items["HidActName"] = ViewState["HidActName"];
        Context.Items["HidActType"] = ViewState["HidActType"];
        Context.Items["HidActStat1"] = ViewState["HidActStat1"];
        Context.Items["HidActStat2"] = ViewState["HidActStat2"];
        Context.Items["HidBeginDate"] = ViewState["HidBeginDate"];
        Context.Items["HidEndDate"] = ViewState["HidEndDate"];
        Context.Items["HidCommand"] = ViewState["HidCommand"];
        Context.Items["HidPageIndex"] = ViewState["HidPageIndex"];

        ControllerUtils.Transfer(Context, url, true);    

    }
}
